package com.sgb.jicai.viewmodel;

import androidx.lifecycle.ViewModel;
import com.sgb.jicai.base.JiCaiBaseApi;
import com.swgk.core.base.api.ICallBack;
import com.sgb.jicai.entity.FactoryCentralizedListEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.view.entity.MenuEntity;
import com.sgb.jicai.entity.JiCaiHomeListRequestBean;
import com.sgb.jicai.entity.FactoryCentralizedItemEntity;
import com.sgb.jicai.view.fragment.HomeJiCaiChangJiaFragment;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HomeJiCaiChangJiaModel extends ViewModel {

    private HomeJiCaiChangJiaFragment fragment;
    private int page = 1;


    public void getFactoryCentralizedList(JiCaiHomeListRequestBean map,boolean loadMore) {
        page = loadMore ? page + 1 : 1;
        map.setCurrent(page);
        map.setLimit(8);
        if (!DialogHelper.isShowing()) {
            DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        }
        JiCaiBaseApi.getApi().getFactoryCentralizedLists(map).enqueue(new ICallBack<BaseEntity<FactoryCentralizedListEntity<FactoryCentralizedItemEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<FactoryCentralizedListEntity<FactoryCentralizedItemEntity>> data) {
                fragment.showList(data.getData().getList(), loadMore);
            }
        });
    }

    /**
     * 供方发起查看H5详情页
     *
     * @param id
     */
    public void getH5Url(String id) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getUrlSetting("Factory").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(JiCaiBaseApi.RESPONSE_OK)) {//type 0 首页 1 我的集采 2 我的报价
                    String url = response.body().getData().getUrl() + "?id=" + id;
//                    String url = "http://192.168.2.33:8067/collection" + "?id=" + id + "&type=0"+"&token="+ JiCaiBaseApi.getToken(); JiCaiBaseApi.getToken()
                    fragment.showUrl(url);
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //获取商品类型
    public void getTypeDatesList(String parentNo, String groupBuyClass) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getSecondCategorys(parentNo, groupBuyClass).enqueue(new Callback<BaseEntity<List<MenuEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<MenuEntity>>> call, Response<BaseEntity<List<MenuEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    fragment.setTypesFirstDatas(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<MenuEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void setActivity(HomeJiCaiChangJiaFragment homeJiCaiChangJiaFragment) {
        this.fragment = homeJiCaiChangJiaFragment;
    }
}
